import { message } from 'antd';
import uuid from 'es6-uuid';

import api from '../../api';

export default {
  namespace: 'limitSetting',
  state: {
    dictMap: {}, // 字典
    list: undefined, // 列表数据
    columns: [
      {
        title: '风险编号',
        dataIndex: 'ruleNo',
        key: 'ruleNo',
      },
      {
        title: '风险类型',
        dataIndex: 'riskCategory',
        key: 'riskCategory',
      },
      {
        title: '风险等级',
        dataIndex: 'riskLevel',
        key: 'riskLevel',
      },
      {
        title: '监管法规',
        dataIndex: 'ordinance',
        key: 'ordinance',
      },
      {
        title: '风险说明',
        dataIndex: 'ruleDesc',
        key: 'ruleDesc',
      },
      {
        title: '作用范围',
        dataIndex: 'ruleApplyObj',
        key: 'ruleApplyObj',
      },
      {
        title: '优先级',
        dataIndex: 'priority',
        key: 'priority',
      },
      {
        title: '是否启用',
        dataIndex: 'ruleState',
        key: 'ruleState',
      },
      {
        title: '比较方向',
        dataIndex: 'compareMethod',
        key: 'compareMethod',
      },
      {
        title: '生效日期',
        dataIndex: 'effectiveTime',
        key: 'effectiveTime',
        type: 'date',
      },
      {
        title: '失效日期',
        dataIndex: 'expireTime',
        key: 'expireTime',
        type: 'date',
      },
    ],
    pagination: {},
    stockList: [],
    toolInfo: [],
    stockFlag: false,
    productSelect: [],
    Factor: [],
    Condition: [],
    productList: [], // 产品下拉
    conditionOriginList: [], // 证券类型 证券类别列表下拉
  },

  effects: {
    // 新增里的 产品下拉
    *fetchRootNode({ payload }, { call }) {
      const response = yield call(api.limitSetting.queryRootNodeAll, payload);
      if (response && response.success) {
        // 增加全部选项
        const arr = [];
        if (response.data) {
          response.data.forEach((item) => {
            arr.push({
              portfolioCode: item.value,
              portfolioName: item.title,
            });
          });
        }
      } else {
        const msg = response && (response.errorMsg || response.message) ? response.errorMsg : '查询失败';
        message.error(msg);
      }
      return response;
    },
    // 查询限额列表
    *fetch({ payload }, { call, put }) {
      const response = yield call(api.limitSetting.queryRules, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            list: response.data || [],
            pagination: {
              current: response.data?.pageInfo?.currentPage,
              pageSize: response.data?.pageInfo?.numInPage,
              total: response.data?.pageInfo?.total,
            },
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            list: undefined,
            pagination: {},
          },
        });
        message.error('查询列表失败！');
      }
    },
    // 规则选择下拉
    *getRulesData({ payload, callback }, { call }) {
      const response = yield call(api.limitSetting.getRulesData, payload);

      if (response && response.code == 0) {
        callback(response.data);
      } else {
        message.error(response?.msg || '获取规则选择下拉列表数据失败!');
      }
    },
    // 计算因子 下拉
    *queryFactorByIndexClass({ payload }, { call }) {
      const response = yield call(api.limitSetting.queryFactorByIndexClass, payload);
      if (response && response.success) {
        response.data?.map((item) => {
          item.value = uuid(16);
          item.disabled = true;
          item.title = item.factorClassType;
          item.children?.map((child) => {
            child.title = child.riskFactorName;
            child.value = child.riskFactorNo.toString();
          });
        });
      }
      return response?.data;
    },
    // 初始化新增页面
    // eslint-disable-next-line no-empty-pattern
    *initAdd({}, { put }) {
      yield put({
        type: 'save',
        payload: {
          data: {
            siftCondition: [],
            siftConditionRelation: ['0'],
            warnTriggerCondition: [],
            warnRuleRelation: ['0'],
            violateTriggerCondition: [],
            violateRuleRelation: ['0'],
          },
        },
      });
    },
    *createRule({ payload }, { call }) {
      const response = yield call(api.limitSetting.createRule, payload);
      return response;
    },
    // 编辑限额
    *modifyRule({ payload }, { call }) {
      const response = yield call(api.limitSetting.modifyRule, payload);
      return response;
    },
    // 删除限额规则
    *deleteRule({ payload }, { call }) {
      const response = yield call(api.limitSetting.deleteRule, payload);
      if (response && response.success) {
        message.success('删除成功');
        return response;
      }
      if (response) {
        message.error(response.message || '删除失败');
      } else {
        message.error('删除失败');
      }
      return null;
    },

    // 查询模板列表
    *queryTemplateList({ payload, callback }, { call }) {
      const res = yield call(api.limitSetting.queryTemplateList, payload);
      if (res && res.success) {
        callback(res.data || []);
      } else {
        callback([]);
        message.error(res?.msg || '查询模板列表失败');
      }
    },
    // 删除模板
    *deleteTemplate({ payload, callback }, { call }) {
      const res = yield call(api.limitSetting.deleteTemplate, payload);
      if (res && res.success) {
        callback();
        message.success('删除模板成功');
      } else {
        message.error(res?.msg || '删除模板失败!');
      }
    },
    // 另存为
    *addTemplate({ payload, callback }, { call }) {
      const res = yield call(api.limitSetting.addTemplate, payload);
      if (res && res.success) {
        callback();
        message.success('模板保存成功');
      } else {
        message.error(res?.msg || '模板保存失败!');
      }
    },

    // 查询比较运算符左侧的下拉
    *queryConditionByFactorCode({ payload }, { call }) {
      const res = yield call(api.limitSetting.queryConditionByFactorCode, payload);
      if (res && res.success) {
        /* empty */
      } else {
        message.error(res?.msg || '查询公式下拉失败');
      }
      return res;
    },
    // 查询比较运算符右侧下拉
    *queryConditionByIndexClassInfo({ payload }, { call }) {
      const res = yield call(api.limitSetting.queryConditionByIndexClassInfo, payload);
      if (res && res.success) {
        /* empty */
      } else {
        message.error(res?.msg || '查询公式下拉失败');
      }
      return res;
    },
    // 查询限额详情
    *queryRuleDatail({ payload, callback }, { call }) {
      const res = yield call(api.limitSetting.queryRuleDatail, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res?.msg || '查询限额详情失败！');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
